<?php
defined('SECUR_CHECK') or die("Security error");
$site->set_templates(array("simple_head.tpl", "session_detail.tpl", "simple_footer.tpl"));
$site->reset_scripts();
$site->add_script("/includes/tiny_mce/jquery.tinymce.js");
$site->add_script("/javascript/load_tinymce_simple.js");
$site->add_script("/javascript/jquery.blockUI.js");
if(isset($_POST['session_id']))
{
    // POST Has been given, save or update session with post values\
    $validator = new validator();
    $rules = array(
        array(
            'name'      =>  'session_date',
            'label'     =>  'date',
            'regexp'    =>  "'(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}'"
        ),
        array(
            'name'      =>  'session_type',
            'label'     =>  'date',
            'matches'   =>  $conf->session_types
        ),
        array(
            'name'      =>  'session_game',
            'label'     =>  'date',
            'matches'   =>  $conf->session_games
        ),
        array(
            'name'      =>  'session_limit',
            'label'     =>  'date',
            'matches'   =>  $conf->session_limits
        )
    );
    $validator->set_rules($rules);
    
    if($validator->validate($_POST))
    {
        // Post values are validated, lets save this session
        if(($_POST['session_id']) != "")
        {
            // Existing session
            $session = new session(intval($_POST['session_id']));
            if($session->owner  ==  $user->id); // Check if current user owns this session
            {
                $session->location  = $_POST['session_location'];
                $session->type      = $_POST['session_type'];
                $session->game      = $_POST['session_game'];
                $session->hours     = $_POST['session_hours'];
                $session->result    = $_POST['session_result'];
                $session->notes     = $_POST['session_notes'];

                /*
                 * Convert date into unix timestamp
                 */                
                $datetime= DateTime::createFromFormat($user->get_option('brm_user_dateformat'), $_POST['session_date']);
                $session->date = $datetime->format("U");

                $session->save();
            }
        }
        else
        {
            // New session
            $session = new session();

            $session->owner     = $user->id;
            $session->location  = $_POST['session_location'];
            $session->type      = $_POST['session_type'];
            $session->game      = $_POST['session_game'];
            $session->hours     = $_POST['session_hours'];
            $session->result    = $_POST['session_result'];
            $session->notes     = $_POST['session_notes'];

            /*
            * Convert date into unix timestamp
            */
            $datetime= DateTime::createFromFormat($user->get_option('brm_user_dateformat'), $_POST['session_date']);
            $session->date = $datetime->format("U");

            $session->save();
        }
    }
}


//Create date format for jquery datepicker, set the default date (today), in case its a new session
$datepicker_format = dateformat_php_to_datepicker($user->get_option("brm_user_dateformat"));
$session_date = date($user->get_option("brm_user_dateformat"),time());
$smarty->assign("SESSION_DATE", $session_date);

// Assign needed variabeles / arrays
$smarty->assign("DATEPICKER_FORMAT", $datepicker_format);
$smarty->assign("SELECT_TYPES", $conf->session_types);
$smarty->assign("SELECT_GAMES", $conf->session_games);
$smarty->assign("SELECT_LIMITS", $conf->session_limits);

if(isset($site->vars[1])) { // Load existing session if set
    $sess_id = intval($site->vars[1]);
    $session = new session($sess_id);
    if($user->level <=0 && $session->owner != $user->id) {
        // Illegal action !
        die('Security error');
    }

    // Convert timestamp into a valid date

    $session_date = date($user->get_option("brm_user_dateformat"),$session->date);

    $smarty->assign("SESSION_ID", $session->id);
    $smarty->assign("SESSION_DATE", $session_date);
    $smarty->assign("SESSION_LOCATION", $session->location);
    $smarty->assign("SESSION_TYPE", $session->type);
    $smarty->assign("SESSION_GAME", $session->game);
    $smarty->assign("SESSION_HOURS", $session->hours);
    $smarty->assign("SESSION_RESULT", $session->result);
    $smarty->assign("SESSION_NOTES", $session->notes);
}


?>
